package com.alex.redis;

import io.rebloom.client.Client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * 布隆过滤器
 * <p>
 * 对于add过的数据, 不会判断失误, 只会对没add的数据, 存在一定得失误率
 */
public class BloomTest {

    private static void init(String blKey, Client client) {
        String fileName = "D:\\download-chrome\\textfilter-master\\textfilter-master\\keywords.txt";
        try (BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));) {
            String temp;
            while ((temp = reader.readLine()) != null) {
                client.add(blKey, temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("数据初始化完成");
    }

    public static void main(String[] args) {
        Client client = new Client("localhost", 6380);
        String blKey = "codehole";
        client.delete(blKey);

        //1.初始化数据
        init(blKey, client);

        //2.校验数据
        System.out.println("校验数据开始");
        validData(client, blKey);
    }

    private static void validData(Client client, String blKey) {

        boolean exists = client.exists(blKey, "我爱巴黎市长");
        if (!exists) {
            System.out.println("存在敏感词");
        }
    }
}
